package utility;

import com.jme.image.Texture;
import com.jme.image.Texture.MagnificationFilter;
import com.jme.image.Texture.MinificationFilter;
import com.jme.image.Texture.WrapMode;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.state.FragmentProgramState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.VertexProgramState;
import com.jme.util.TextureManager;

public class BumpMap {
	private TextureState 		 texture;
	private VertexProgramState 	 vert;
	private FragmentProgramState frag;
	
	public BumpMap(String imgPath,
			       String normPath, 
			       String bumpPath,
			       String vertexPath,
			       String fragPath, 
			       Renderer renderer)
	{
		texture = renderer.createTextureState();
		
		//Image Texture
		Texture tex = TextureManager.loadTexture(BumpMap.class.getResource(imgPath));
		tex.setMinificationFilter(MinificationFilter.Trilinear);
		tex.setMagnificationFilter(MagnificationFilter.Bilinear);
		tex.setScale(new Vector3f(2, 2, 2));
		tex.setWrap(WrapMode.Repeat);
		
		//Bump Texture
		Texture height = TextureManager.loadTexture(BumpMap.class.getResource(bumpPath));
		height.setMinificationFilter(MinificationFilter.Trilinear);
		height.setMagnificationFilter(MagnificationFilter.Bilinear);
		height.setScale(new Vector3f(2, 2, 2));
		height.setWrap(WrapMode.Repeat);
		
		//Normals Texture
		Texture normal = TextureManager.loadTexture(BumpMap.class.getResource(normPath));
		normal.setMinificationFilter(MinificationFilter.Trilinear);
		normal.setMagnificationFilter(MagnificationFilter.Bilinear);
		normal.setScale(new Vector3f(2, 2, 2));
		normal.setWrap(WrapMode.Repeat);
		
		//Unisco le Textures
		texture.setTexture(tex, 0); 	//layer 0
		texture.setTexture(normal,1); 	//layer 1
		texture.setTexture(height,2); 	//layer 2
		texture.setEnabled(true);
		
		vert = renderer.createVertexProgramState();
		frag = renderer.createFragmentProgramState();
		
		vert.load(BumpMap.class.getClassLoader().getResource(vertexPath));
        vert.setEnabled(true);
        
        frag.load(BumpMap.class.getClassLoader().getResource(fragPath));
        frag.setEnabled(true);
	}
	
	public TextureState getTexture()
	{
		return texture;
	}
	public VertexProgramState getVertex()
	{
		return vert;
	}
	public FragmentProgramState getFragment()
	{
		return frag;
	}
}
